package com.shoppingmall.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shoppingmall.commons.PageParam;
import com.shoppingmall.mapper.CommentMapper;
import com.shoppingmall.mapper.GoodsMapper;
import com.shoppingmall.model.Goods;
import com.shoppingmall.model.view.CommentView;
import com.shoppingmall.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;
    private CommentMapper commentMapper;
    @Autowired
    public GoodsServiceImpl(GoodsMapper goodsMapper, CommentMapper commentMapper) {
        this.goodsMapper = goodsMapper;
        this.commentMapper = commentMapper;
    }
    @Override
    public List<Goods> queryGoods(Integer...isDisplay) {
        return goodsMapper.queryGood(isDisplay);
    }

    @Override
    public Goods queryGoodById(int goods_id) {
        return goodsMapper.queryGoodById(goods_id);
    }

    @Override
    public void addGoods(Goods goods) {
        goodsMapper.addGoods(goods);
    }

    @Override
    public void updateGoods(Goods goods) {
       goodsMapper.updateGoods(goods);
    }

    @Override
    public void delGoods(int...goods_ids) {
       goodsMapper.delGoods(goods_ids);
    }

    @Override
    public List<Goods> queryGoodsByName(String goods_name) {
        return goodsMapper.queryGoodsByName(goods_name);
    }

    @Override
    public List<Goods> queryGoodsByKind(int goods_kind) {
        return goodsMapper.queryGoodsByKind(goods_kind);
    }

    @Override
    public void updatemangerGoods(int goods_display,int goods_id) {
        goodsMapper.updatemangerGoods(goods_display,goods_id);
    }

    @Override
    public List<Goods> mangerqueryGood(int goods_display) {
        System.out.println("-------------------------------------------------------------");
        System.out.println(goods_display);
        return goodsMapper.mangerqueryGood( goods_display);
    }

    @Override
    public Map<String, Object> queryCommentByGoodsId(int goods_id, PageParam pageParam) {
        //使用PageHelper实现分页,实现分页的三段代码不能分割
        //开启分页插件
        PageHelper.startPage(pageParam);
        //查询数据
        List<CommentView> commentViews = commentMapper.getCommentByBookId(goods_id);
        //将查询结果封装到PageInfo对象中,该对象是PageHelper提供
        //pageInfo对象会根据commentViews参数封装分页相关数据
        PageInfo pageInfo = new PageInfo(commentViews);

        //将分页所需要返回到客户端的数据封装到Map集合中
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("commentList",pageInfo.getList());
        return map;
    }
}
